package com.zili.sink;

import com.zili.source.Event;
import org.apache.flink.api.common.serialization.SimpleStringEncoder;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.filesystem.StreamingFileSink;
import org.apache.flink.streaming.api.functions.sink.filesystem.rollingpolicies.DefaultRollingPolicy;

import java.util.concurrent.TimeUnit;

/**
 * @author : ranzlupup
 * @since : 2023/3/8 15:14
 */
public class SinkToFileTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStreamSource<Event> stream = env.fromElements(
                new Event("Mary1", "./home", 1000L),
                new Event("Bob2", "./cart", 2000L),
                new Event("Alice3", "./prod?id=1", 3000L),
                new Event("Cary4", "./home", 4000L),
                new Event("Mary5", "./home", 1000L),
                new Event("Bob6", "./cart", 2000L),
                new Event("Alice7", "./prod?id=1", 3000L),
                new Event("Cary8", "./home", 4000L),
                new Event("Mary9", "./home", 1000L),
                new Event("Bob2", "./cart", 2000L),
                new Event("Alice3", "./prod?id=1", 3000L),
                new Event("Cary4", "./home", 4000L)
        );
        StreamingFileSink<String> streamingFileSink = StreamingFileSink
                .<String>forRowFormat(

                        new Path("./output"),
                        new SimpleStringEncoder<>("UTF-8"))
                .withRollingPolicy(
                        DefaultRollingPolicy.builder()
                                .withRolloverInterval(TimeUnit.MINUTES.toMillis(15))
                                .withInactivityInterval(TimeUnit.MINUTES.toMillis(5))
                                .withMaxPartSize(1024 * 1024 * 1024)
                                .build()
                )
                .build();

        stream.map(Event::toString).addSink(streamingFileSink);

        env.execute();
    }
}
